﻿Imports System.Text
Imports System.Windows.Threading
Imports System.Reflection

Partial Public Class MainPage
  Inherits UserControl

  Public Sub New()
    InitializeComponent()
  End Sub

  Dim cRandom As New MTRandom
  Dim cCuentas As New Dictionary(Of String, CuentaControl)
  Dim DTimer As System.Windows.Threading.DispatcherTimer
  Dim stStart As DateTime
  Dim bRun As Boolean = False
  Dim bPause As Boolean = False
    Dim Clave As Integer = 0
  Dim fileid As String

  Private Sub btGenerar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btGenerar.Click
    Try
      spCuentas.Children.Clear()
      cRandom.init_random(True)

      fileid = cRandom.genrand_int32()

      txID.Text = fileid
      txID.Visibility = Visibility.Visible
      'Genero Sumas


      For i As Integer = 1 To QSumas.Value
        Dim ThisCuenta As New CuentaControl(String.Format("Suma:{0:00}", i).ToString, CuentaControl.CuentaType.Suma)
        ThisCuenta.FileID = fileid
        Dim sArg As New StringBuilder

        ThisCuenta.Argumento1 = cRandom.genrand_intRange(Math.Pow(10, DSumasOp1.Value - 1), Math.Pow(10, DSumasOp1.Value) - 1)
        If Clave < ThisCuenta.Argumento1 Then Clave = ThisCuenta.Argumento1

        ThisCuenta.Argumento2 = cRandom.genrand_intRange(Math.Pow(10, DSumasOp2.Value - 1), Math.Pow(10, DSumasOp2.Value) - 1)
        If Clave < ThisCuenta.Argumento2 Then Clave = ThisCuenta.Argumento2
        spCuentas.Children.Add(ThisCuenta)

      Next

      'Genero Restas

      For i As Integer = 1 To QRestas.Value
        Dim ThisCuenta As New CuentaControl(String.Format("Resta:{0:00}", i).ToString, CuentaControl.CuentaType.Resta)
        ThisCuenta.FileID = fileid
        Dim sArg As New StringBuilder
        Dim iArg1, iArg2 As Integer
        iArg1 = cRandom.genrand_intRange(Math.Pow(10, DRestasOp1.Value - 1), Math.Pow(10, DRestasOp1.Value) - 1)
        If Clave < iArg1 Then Clave = iArg1
        sArg.Length = 0
        iArg2 = cRandom.genrand_intRange(Math.Pow(10, DRestasOp2.Value - 1), Math.Pow(10, DRestasOp2.Value) - 1)
        If Clave < iArg2 Then Clave = iArg2
        If iArg2 > iArg1 Then
          ThisCuenta.Argumento1 = CInt(iArg2.ToString)
          ThisCuenta.Argumento2 = CInt(iArg1.ToString)
        Else
          ThisCuenta.Argumento1 = CInt(iArg1.ToString)
          ThisCuenta.Argumento2 = CInt(iArg2.ToString)
        End If
        spCuentas.Children.Add(ThisCuenta)
      Next

      'Genero Multiplicacion

      For i As Integer = 1 To QMulti.Value
        Dim ThisCuenta As New CuentaControl(String.Format("Multi:{0:00}", i).ToString, CuentaControl.CuentaType.Multiplicacion)
        ThisCuenta.FileID = fileid
        Dim sArg As New StringBuilder
        Dim iArg1, iArg2 As Integer
        iArg1 = cRandom.genrand_intRange(Math.Pow(10, DMultiOp1.Value - 1), Math.Pow(10, DMultiOp1.Value) - 1)
        If Clave < iArg1 Then Clave = iArg1
        iArg2 = cRandom.genrand_intRange(DMultixMin.Value, DMultixMax.Value)
        ThisCuenta.Argumento1 = CInt(iArg1.ToString)
        ThisCuenta.Argumento2 = CInt(iArg2.ToString)
        spCuentas.Children.Add(ThisCuenta)
      Next

      'Genero Division

      For i As Integer = 1 To QDivi.Value
        Dim ThisCuenta As New CuentaControl(String.Format("Divi:{0:00}", i).ToString, CuentaControl.CuentaType.Dividir)
        ThisCuenta.FileID = fileid
        Dim sArg As New StringBuilder
        Dim iArg1, iArg2 As Integer
        iArg1 = cRandom.genrand_intRange(Math.Pow(10, DDiviOp1.Value - 1), Math.Pow(10, DDiviOp1.Value) - 1)
        If Clave < iArg1 Then Clave = iArg1
        iArg2 = cRandom.genrand_intRange(DDivixMin.Value, DDivixMax.Value)
        ThisCuenta.Argumento1 = CInt(iArg1.ToString)
        ThisCuenta.Argumento2 = CInt(iArg2.ToString)
        spCuentas.Children.Add(ThisCuenta)
      Next

      'Genero Sumas con Reales
      If (DcSumasOp1.Value + DcSumasOp2.Value) > 0 Then
        For i As Integer = 1 To QRSumas.Value

          Dim ThisCuenta As New CuentaControl(String.Format("Suma:{0:00}", i).ToString, CuentaControl.CuentaType.Suma)
          ThisCuenta.FileID = fileid
          Dim sArg As New StringBuilder

          ThisCuenta.DcArgumento1 = CInt(DcSumasOp1.Value)
          ThisCuenta.Argumento1 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRSumasOp1.Value - 1 + DcSumasOp1.Value), Math.Pow(10, DRSumasOp1.Value + DcSumasOp1.Value) - 1) / Math.Pow(10, DcSumasOp1.Value)), CInt(DcSumasOp1.Value))

          If Clave < ThisCuenta.Argumento1 Then Clave = ThisCuenta.Argumento1

          ThisCuenta.DcArgumento2 = CInt(DcSumasOp2.Value)
          ThisCuenta.Argumento2 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRSumasOp2.Value - 1 + DcSumasOp2.Value), Math.Pow(10, DRSumasOp2.Value + DcSumasOp2.Value) - 1) / Math.Pow(10, DcSumasOp2.Value)), CInt(DcSumasOp2.Value))
          If Clave < ThisCuenta.Argumento2 Then Clave = ThisCuenta.Argumento2
          spCuentas.Children.Add(ThisCuenta)

        Next
      End If

      'Genero Restas Reales
      If (DcRestasOp1.Value + DcRestasOp2.Value) > 0 Then

        For i As Integer = 1 To QRRestas.Value
          Dim ThisCuenta As New CuentaControl(String.Format("Resta:{0:00}", i).ToString, CuentaControl.CuentaType.Resta)
          ThisCuenta.FileID = fileid
          Dim sArg As New StringBuilder
          Dim iArg1, iArg2 As Double
          iArg1 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRRestasOp1.Value - 1 + DcRestasOp1.Value), Math.Pow(10, DRRestasOp1.Value + DcRestasOp1.Value) - 1) / Math.Pow(10, DcRestasOp1.Value)), CInt(DcRestasOp1.Value))
          If Clave < iArg1 Then Clave = iArg1
          sArg.Length = 0
          iArg2 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRRestasOp2.Value - 1 + DcRestasOp2.Value), Math.Pow(10, DRRestasOp2.Value + DcRestasOp2.Value) - 1) / Math.Pow(10, DcRestasOp2.Value)), CInt(DcRestasOp2.Value))
          If Clave < iArg2 Then Clave = iArg2
          If iArg2 > iArg1 Then
            ThisCuenta.DcArgumento1 = CInt(DcRestasOp2.Value)
            ThisCuenta.DcArgumento2 = CInt(DcRestasOp1.Value)
            ThisCuenta.Argumento1 = iArg2
            ThisCuenta.Argumento2 = iArg1
          Else
            ThisCuenta.DcArgumento1 = CInt(DcRestasOp1.Value)
            ThisCuenta.DcArgumento2 = CInt(DcRestasOp2.Value)
            ThisCuenta.Argumento2 = iArg2
            ThisCuenta.Argumento1 = iArg1
          End If
          spCuentas.Children.Add(ThisCuenta)
        Next
      End If

      'Genero Reales Multiplicacion
      If (DcMultiOp1.Value + DcMultix.Value) > 0 Then
        For i As Integer = 1 To QRMulti.Value
          Dim ThisCuenta As New CuentaControl(String.Format("Multi:{0:00}", i).ToString, CuentaControl.CuentaType.Multiplicacion)
          ThisCuenta.FileID = fileid
          Dim sArg As New StringBuilder
          Dim iArg1, iArg2 As Double
          iArg1 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRMultiOp1.Value - 1 + DcMultiOp1.Value), Math.Pow(10, DRMultiOp1.Value + DcMultiOp1.Value) - 1) / Math.Pow(10, DcMultiOp1.Value)), CInt(DcMultiOp1.Value))
          If Clave < iArg1 Then Clave = iArg1
          iArg2 = Math.Round(CDbl(cRandom.genrand_intRange(DMultixMin.Value * Math.Pow(10, DcMultix.Value), DMultixMax.Value * Math.Pow(10, DcMultix.Value)) / Math.Pow(10, DcMultix.Value)), CInt(DcMultix.Value))
          ThisCuenta.DcArgumento1 = CInt(DcMultiOp1.Value)
          ThisCuenta.DcArgumento2 = CInt(DcMultix.Value)
          ThisCuenta.Argumento1 = iArg1.ToString
          ThisCuenta.Argumento2 = iArg2.ToString
          spCuentas.Children.Add(ThisCuenta)
        Next
      End If

      'Genero Reales Division
      If (DcDiviOp1.Value + DcDivix.Value) > 0 Then
        For i As Integer = 1 To QRDivi.Value
          Dim ThisCuenta As New CuentaControl(String.Format("Divi:{0:00}", i).ToString, CuentaControl.CuentaType.Dividir)
          ThisCuenta.FileID = fileid
          Dim sArg As New StringBuilder
          Dim iArg1, iArg2 As Double
          iArg1 = Math.Round(CDbl(cRandom.genrand_intRange(Math.Pow(10, DRDiviOp1.Value - 1 + DcDiviOp1.Value), Math.Pow(10, DRDiviOp1.Value + DcDiviOp1.Value) - 1) / Math.Pow(10, DcDiviOp1.Value)), CInt(DcDiviOp1.Value))
          If Clave < iArg1 Then Clave = iArg1
          iArg2 = Math.Round(CDbl(cRandom.genrand_intRange(DRDivixMin.Value * Math.Pow(10, DcDivix.Value), DRDivixMax.Value * Math.Pow(10, DcDivix.Value)) / Math.Pow(10, DcDivix.Value)), CInt(DcDivix.Value))
          ThisCuenta.DcArgumento1 = CInt(DcDiviOp1.Value)
          ThisCuenta.DcArgumento2 = CInt(DcDivix.Value)
          ThisCuenta.Argumento1 = iArg1.ToString
          ThisCuenta.Argumento2 = iArg2.ToString
          spCuentas.Children.Add(ThisCuenta)
        Next
      End If

      OpEnteros.IsExpanded = False
      OpReales.IsExpanded = False
      OpGnrl.IsExpanded = False

    Catch ex As Exception
      LoggingErr(ex)

    End Try
  End Sub

  Private Sub btValidar_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btValidar.Click
    Try

      xAdmin.Visibility = Visibility.Visible
      xAdmin.Focus()
    Catch ex As Exception
      LoggingErr(ex)

    End Try
  End Sub

  Private Sub xAdmin_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles xAdmin.KeyUp
    Try
      If e.Key = Key.Ctrl AndAlso (xAdmin.Password.Trim.ToLower() = "Keego".Trim.ToLower() OrElse xAdmin.Password.Trim = Clave OrElse xAdmin.Password.Trim.ToLower() = txID.Text.Trim.ToLower()) Then
        ShowSolutions()
      Else
        HideSolutions()
      End If
    Catch ex As Exception
      LoggingErr(ex)

    End Try

  End Sub

  Private Sub HideSolutions()
    Try

      For Each ThisCuenta As CuentaControl In spCuentas.Children
        ThisCuenta.HideSolucion()
      Next

    Catch ex As Exception
      LoggingErr(ex)

    End Try

  End Sub

  Private Sub ShowSolutions()
    Try

      For Each ThisCuenta As CuentaControl In spCuentas.Children
        ThisCuenta.ShowSolucion()
      Next

    Catch ex As Exception
      LoggingErr(ex)

    End Try

  End Sub

  Private Sub DTimer_Tick()
    Try

      Dim tmelpased As TimeSpan = stStart.AddMinutes(QTiempo.Value) - Now

      xTime.Text = tmelpased.ToString
      With BarTime
        .Minimum = 0
        .Maximum = 60 * 1000 * QTiempo.Value
        .Value = tmelpased.TotalMilliseconds
      End With

      If tmelpased.TotalMilliseconds < 0 Then
        xTime.Text = "00:00:00"
        With BarTime
          .Minimum = 0
          .Maximum = 60 * 1000 * QTiempo.Value
          .Value = 0
        End With
        vuvuzela.Play()
        DTimer.Stop()
      End If

    Catch ex As Exception
      LoggingErr(ex)

    End Try

  End Sub

  Private Sub MainPage_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
    If Keyboard.Modifiers = (ModifierKeys.Control + ModifierKeys.Alt) AndAlso e.Key = System.Windows.Input.Key.R Then
      Dim sValues As String = Hidrate(fileid)

      Clipboard.SetText(sValues)

    End If
    If Keyboard.Modifiers = (ModifierKeys.Control + ModifierKeys.Alt) AndAlso e.Key = System.Windows.Input.Key.D Then
      ClearIS()
    End If
  End Sub

  Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Try
      DTimer = New DispatcherTimer()
      AddHandler DTimer.Tick, AddressOf DTimer_Tick
      DTimer.Interval = TimeSpan.FromMilliseconds(137)
      DTimer.Stop()
      XpTiempo.Visibility = Windows.Visibility.Collapsed

      xTime.Text = String.Format("{0:00}:00:000", QTiempo.Value).ToString
      With BarTime
        .Minimum = 0
        .Maximum = 60 * 1000 * QTiempo.Value
        .Value = 60 * 1000 * QTiempo.Value
      End With
    Catch ex As Exception
      LoggingErr(ex)

    End Try
    
  End Sub

  Private Sub btControl_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btControl.Click
    Try

      Select Case bRun
        Case True
          DTimer.Stop()
          btPause.Visibility = Visibility.Collapsed
        Case Else
          XpTiempo.Visibility = Windows.Visibility.Visible
          stStart = Now
          xTime.Text = String.Format("{0:00}:00:000", QTiempo.Value).ToString
          With BarTime
            .Minimum = 0
            .Maximum = 60 * 1000 * QTiempo.Value
            .Value = 60 * 1000 * QTiempo.Value
          End With
          DTimer.Start()
          btPause.Visibility = Visibility.Visible
          ' vuvuzela.Play()
      End Select

      bRun = Not bRun

    Catch ex As Exception
      LoggingErr(ex)

    End Try
  End Sub

  Private Sub btPause_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btPause.Click
    Try

      If Not bRun Then Exit Try

      Select Case bPause
        Case False
          DTimer.Stop()
        Case True
          DTimer.Start()
      End Select

      bPause = Not bPause

    Catch ex As Exception
      LoggingErr(ex)

    End Try
  End Sub

  Private Sub MainPage_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
    'ClearIS()
  End Sub

  Private Sub xAdmin_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles xAdmin.Loaded
    Cp.Text = String.Format("{0} {1} {2}", Assembly.GetExecutingAssembly().FullName.Split(",")(0), Assembly.GetExecutingAssembly().FullName.Split(",")(1), CType(Assembly.GetExecutingAssembly().GetCustomAttributes(GetType(System.Reflection.AssemblyCopyrightAttribute), False)(0), System.Reflection.AssemblyCopyrightAttribute).Copyright).ToString()


  End Sub

  Private Sub QZoom_ValueChanged(sender As System.Object, e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles QZoom.ValueChanged
    Dim trSc As New ScaleTransform()
    Dim trval As Double = 1.0

    Select Case QZoom.Value
      Case 5
        trval = 4
      Case 4
        trval = 3
      Case 3
        trval = 2
      Case 2
        trval = 1.5
      Case 1
        trval = 1.25
      Case 0
        trval = 1.0
      Case -5
        trval = 0.25
      Case -4
        trval = 0.5
      Case -3
        trval = 0.75
      Case -2
        trval = 0.8
      Case -1
        trval = 0.9
      Case Else
        trval = 1.0
    End Select
    trSc.ScaleX = trval
    trSc.ScaleY = trval
    LayoutRoot.RenderTransform = trSc
  End Sub
End Class